package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.liliudong.template.biz.service.sys.SysRoleService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.request.sys.SysRoleAddRequest;
import com.liliudong.template.common.request.sys.SysRoleModifyRequest;
import com.liliudong.template.common.entity.sys.SysRole;
import com.liliudong.template.common.response.sys.SysRoleDetailResponse;
import com.liliudong.template.common.response.sys.SysRoleListResponse;
import com.liliudong.template.common.response.sys.SysRolePageResponse;
import com.mybatisflex.core.paginate.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色
 *
 * @author ldli5
 * @classname SysRoleController
 * @date 2024/6/20-13:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 添加一个
     *
     * @param sysRoleAddRequest sys角色添加dto
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:add")
    @PostMapping
    public R<?> addOne(@RequestBody @Validated SysRoleAddRequest sysRoleAddRequest) {
        sysRoleService.addOne(sysRoleAddRequest);
        return R.ok();
    }

    /**
     * 删除一个
     *
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:delete")
    @DeleteMapping("/{id}")
    public R<?> deleteOne(@PathVariable Long id) {
        sysRoleService.deleteById(id);
        return R.ok();
    }

    /**
     * 更新一个
     *
     * @param sysRoleModifyRequest sys角色更新dto
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:modify")
    @PutMapping("/{id}")
    public R<?> modifyById(@PathVariable Long id, @RequestBody @Validated SysRoleModifyRequest sysRoleModifyRequest) {
        sysRoleService.modifyById(id, sysRoleModifyRequest);
        return R.ok();
    }


    /**
     * 分页查询
     *
     * @param page 页面
     * @param size 大小
     * @return {@link R }<{@link Page }<{@link SysRolePageResponse }>>
     */
    @SaCheckPermission("sys:role:list")
    @GetMapping("/page/{page}/{size}")
    public R<Page<SysRolePageResponse>> page(@PathVariable Long page,
                                             @PathVariable Long size) {
        Page<SysRolePageResponse> sysRolePage = sysRoleService.queryPage(page, size);
        return R.ok(sysRolePage);
    }

    /**
     * 全部
     *
     * @return {@link R }<{@link List }<{@link SysRole }>>
     */
    @SaCheckPermission("sys:role:list")
    @GetMapping("/list")
    public R<List<SysRoleListResponse>> list() {
        List<SysRoleListResponse> sysRoleListVOS = sysRoleService.listAs(null, SysRoleListResponse.class);
        return R.ok(sysRoleListVOS);
    }

    /**
     * id明细
     *
     * @param id 身份证件
     * @return {@link R }<{@link SysRoleDetailResponse }>
     */
    @SaCheckPermission("sys:role:detail")
    @GetMapping("/detail/{id}")
    public R<SysRoleDetailResponse> detailById(@PathVariable Long id) {
        SysRoleDetailResponse sysRoleDetailVO = sysRoleService.detailById(id);
        return R.ok(sysRoleDetailVO);
    }
}
